// pages/appLetWork/work7/weather.js
var baseurl = "https://devapi.qweather.com/v7/weather/3d?key=d23c88c3baa44bc29d7e7b01d31e0c97&location=";
var cityUrl = "https://geoapi.qweather.com/v2/city/lookup?key=d23c88c3baa44bc29d7e7b01d31e0c97&location=";
var lifeUrl = "https://devapi.qweather.com/v7/indices/1d?type=0&key=d23c88c3baa44bc29d7e7b01d31e0c97&location=";
var now = "https://devapi.qweather.com/v7/weather/now?key=d23c88c3baa44bc29d7e7b01d31e0c97&location=";

Page({
  data: {
    defaultLocation: "广州",
    location: " ",
    cityId: "",
    lifeExponent: [],
    lifeExponentImg: [1, 2, 3, 4, 5, 6, 7, 8],
    toDayTemp: "0",
    toDayText: "无",
    toDayCardBottom: ["风向","相对湿度", "体感温度", "气压"],//风向、相对温度、体感温度、气压
    toDayCardBottom_text: [],//对应风向、相对温度、体感温度、气压
    toDayIcon: "http://8.142.44.85:2026/102.svg",
    ThreeDayWeather: "",
    ThreeDayText: ["今天", "明天", "后天"],
    isShowDialog: false,
    air_quality: ""//空气质量
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },

  //页面周期函数 页面初始加载
  onLoad: function (options) {
    wx.request({
      url: cityUrl + this.data.defaultLocation,
      success: (res) => {
        this.setData({
          location: res.data.location[0].name,
          cityId: res.data.location[0].id
        })
        //三天的
        wx.request({
          url: baseurl + res.data.location[0].id,
          success: (weather) => {
            //console.log(weather.data.daily)
            this.setData({
              'ThreeDayWeather[0]': weather.data.daily[0],
              'ThreeDayWeather[1]': weather.data.daily[1],
              'ThreeDayWeather[2]': weather.data.daily[2],
            })
            //console.log(this.data.ThreeDayWeather)
          }
        }),
          //生活指数的
          wx.request({
            url: lifeUrl + res.data.location[0].id,
            success: (life) => {
              // console.log(life.data.daily);
              this.setData({
                'lifeExponent[0]': life.data.daily[8],//舒适度指数
                'lifeExponent[1]': life.data.daily[10],//穿衣指数
                'lifeExponent[2]': life.data.daily[12],//感冒指数
                'lifeExponent[3]': life.data.daily[15],//运动指数
                'lifeExponent[4]': life.data.daily[3],//旅游指数
                'lifeExponent[5]': life.data.daily[1],//紫外线指数
                'lifeExponent[6]': life.data.daily[13],//洗车指数
                'lifeExponent[7]': life.data.daily[9],//空气污染扩散条件指数
                air_quality: life.data.daily[9].category,//空气质量
              })
            }
          })
        //当天的
        wx.request({
          url: now + res.data.location[0].id,
          success: (nowtop) => {
            // console.log(nowtop.data.now)
            var carbottom = new Array(nowtop.data.now.windDir, "相对湿度", "体感温度","气压")
            this.setData({
              toDayTemp: nowtop.data.now.temp,
              toDayIcon: "http://8.142.44.85:2026/" + (nowtop.data.now.icon) + ".svg",
              toDayText: nowtop.data.now.text,
              toDayCardBottom: carbottom,
              toDayCardBottom_text: new Array(nowtop.data.now.windScale + "级", nowtop.data.now.humidity + "%", nowtop.data.now.feelsLike + "°" , nowtop.data.now.pressure + "kPa")
            })
          }

        })
      }
    })
  },
  cityChange({ detail }) {
    this.setData({
      location: detail,
    })
    if (this.data.location == "") {
      this.setData({
        location: this.data.defaultLocation
      })
      this.onLoad();
    }
  },

  search() {
    wx.request({
      url: cityUrl + this.data.location,
      success: (res) => {
        this.setData({
          location: res.data.location[0].name,
          cityId: res.data.location[0].id
        })
        //三天的
        wx.request({
          url: baseurl + res.data.location[0].id,
          success: (weather) => {
            this.setData({
              'ThreeDayWeather[0]': weather.data.daily[0],
              'ThreeDayWeather[1]': weather.data.daily[1],
              'ThreeDayWeather[2]': weather.data.daily[2],
            })
            //console.log(this.data.ThreeDayWeather)
          }
        }),
          //生活指数的
          wx.request({
            url: lifeUrl + res.data.location[0].id,
            success: (life) => {
              this.setData({
                'lifeExponent[0]': life.data.daily[8],//舒适度指数
                'lifeExponent[1]': life.data.daily[10],//穿衣指数
                'lifeExponent[2]': life.data.daily[12],//感冒指数
                'lifeExponent[3]': life.data.daily[15],//运动指数
                'lifeExponent[4]': life.data.daily[3],//旅游指数
                'lifeExponent[5]': life.data.daily[1],//紫外线指数
                'lifeExponent[6]': life.data.daily[13],//洗车指数
                'lifeExponent[7]': life.data.daily[9],//空气污染扩散条件指数
                air_quality: life.data.daily[9].category,//空气质量
              })
              //console.log(this.data.lifeExponent)
            }
          }),
          //当天的
          wx.request({
            url: now + res.data.location[0].id,
            success: (nowtop) => {
              //console.log(nowtop.data.now)
              var carbottom = new Array(nowtop.data.now.windDir, "相对湿度", "体感温度","气压")
              this.setData({
                toDayTemp: nowtop.data.now.temp,
                toDayIcon: "http://8.142.44.85:2026/" + (nowtop.data.now.icon) + ".svg",
                toDayText: nowtop.data.now.text,
                toDayCardBottom: carbottom,
                toDayCardBottom_text: new Array(nowtop.data.now.windScale + "级", nowtop.data.now.humidity + "%", nowtop.data.now.feelsLike + "°" ,nowtop.data.now.pressure + "kPa")
              })
            }
          })
      }
    })
  },

  /**
   * 搜索图标
   */
  searchIcon() {
    this.setData({ isShowDialog: true })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


})